﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Scriptor.Presentation.Entity;

namespace Scriptor.Presentation.Templates
{
    public class DCTemplate
    {
        List<string> gUsing { get; set; }
        public StringBuilder clausulaUsingFormatada { get; set; }

        List<string> gDContract { get; set; }
        public StringBuilder dataContractFormatado { get; set; }

        List<string> gPropertie { get; set; }
        public StringBuilder propertieDCFormatada { get; set; }

        public string strUsing()
        {
            gUsing = new List<string>();

            gUsing.Add("using System.Runtime.Serialization;");

            clausulaUsingFormatada = new StringBuilder();

            foreach (string str in gUsing)
            {
                clausulaUsingFormatada.Append(str);
                clausulaUsingFormatada.Append(Environment.NewLine);
            }

            return clausulaUsingFormatada.ToString();
        }

        public string strDContract(string pNameSpace, string pClassName, bool showDecorators)
        {
            gDContract = new List<string>();

            gDContract.Add("namespace " + pNameSpace);
            gDContract.Add("{");
            
            if(showDecorators)
            gDContract.Add("\t[DataContract(Namespace = \"http://www.bovespa.com.br/" + pNameSpace + "\", Name =\"" + pClassName + "\")]");

            gDContract.Add("\tpublic class " + pClassName);
            gDContract.Add("\t{");
            gDContract.Add("@@class-body");
            gDContract.Add("\t}");
            gDContract.Add("}");

            dataContractFormatado = new StringBuilder();

            foreach (string str in gDContract)
            {
                dataContractFormatado.Append(str);
                dataContractFormatado.Append(Environment.NewLine);
            }

            return dataContractFormatado.ToString();
        }

        public string strPropertie(Propriedades prpExterna,Entidade ent, bool showDecorators)
        {
            string concat = string.Empty;

            if (prpExterna.TipoPropriedade.IndexOf("Enum") != -1)
                concat = "DataContract.";

            Propriedades prp = new Propriedades(prpExterna.NomePropriedade, concat + prpExterna.TipoPropriedade);

            gPropertie = new List<string>();

            string open = "\t\t{";
            string close = "\t\t}";

            gPropertie.Add(string.Format("\t\tprivate {0} _{1};", prp.TipoPropriedade, prp.NomePropriedade.Substring(0, 1).ToLower() + prp.NomePropriedade.Substring(1)));

            if (showDecorators)
            {
                gPropertie.Add("\t\t[DataMember(IsRequired = false)]");
            }

            gPropertie.Add(string.Format("\t\tpublic {0} {1}",prp.TipoPropriedade,prp.NomePropriedade));
            gPropertie.Add(open);
            gPropertie.Add("\t\t\tget { return _@@prop; }".Replace("@@prop", prp.NomePropriedade.Substring(0, 1).ToLower() + prp.NomePropriedade.Substring(1)));
            gPropertie.Add("\t\t\tset { _@@prop = value; }".Replace("@@prop", prp.NomePropriedade.Substring(0, 1).ToLower() + prp.NomePropriedade.Substring(1)));
            gPropertie.Add(close);

            propertieDCFormatada = new StringBuilder();

            foreach (string str in gPropertie)
            {
                propertieDCFormatada.Append(str);
                propertieDCFormatada.Append(Environment.NewLine);
            }

            propertieDCFormatada.Append(Environment.NewLine);

            return propertieDCFormatada.ToString();
        }

        public string AppendProperties(string pClassScope, StringBuilder stb)
        {
            return pClassScope.ToString().Replace("@@class-body", stb.ToString());
        }
    }
}
